/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.editor.editparts;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

public class OperatorFigure extends EditableFigure {

    private static final Color textColor = new Color(null, 255, 255, 206);
    private static final Color exdataColor = new Color(null, 206, 206, 255);
    private static final Color errorColor = new Color(null, 255, 206, 206);
    private static final Font nameFont = new Font(null, "Arial", 12, SWT.BOLD);

    private Label nameLabel = makeNameLabel();
    private CompartmentFigure textFigure = new CompartmentFigure(textColor,
            new InsetLineBorder());
    private CompartmentFigure errorFigure = new CompartmentFigure(errorColor,
            new InsetLineBorder());
    private CompartmentFigure exdataFigure = new CompartmentFigure(exdataColor,
            new InsetLineBorder());
    private CompartmentFigure aliasFigure = new CompartmentFigure(
            ColorConstants.white, new InsetNoBorder());

    private List<CompartmentFigure> textFigures = new LinkedList<CompartmentFigure>();

    // new addition
    private CompartmentFigure textFigure1 = new CompartmentFigure(textColor,
            new InsetLineBorder());

    public OperatorFigure() {
        ToolbarLayout layout = new ToolbarLayout();
        setLayoutManager(layout);

        layout.setVertical(true);

        setOpaque(false);

        add(nameLabel);
        add(textFigure);

        // add(textFigure1);

        add(aliasFigure);

        textFigures.add(textFigure);
        textFigures.add(textFigure1);
    }

    private void addErrorFigure() {
        try {
            remove(errorFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }

        try {
            remove(exdataFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }

        try {
            remove(aliasFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }

        add(errorFigure);
        add(aliasFigure);
    }

    private void addExdataFigure() {
        try {
            remove(errorFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }

        try {
            remove(exdataFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }

        try {
            remove(aliasFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }

        add(exdataFigure);
        add(aliasFigure);

    }

    private void removeErrorFigure() {
        try {
            remove(errorFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }
    }

    private void removeExdataFigure() {
        try {
            remove(exdataFigure);
        } catch (Throwable e) {
            // if the figure wasn't there in the first place, then fine. (we
            // want this action to be idempotent)
        }
    }

    public class InsetLineBorder extends LineBorder {
        public Insets getInsets(IFigure figure) {
            return new Insets(2, 4, 2, 4);
        }
    }

    public class InsetNoBorder extends AbstractBorder {
        public Insets getInsets(IFigure figure) {
            return new Insets(2, 4, 2, 4);
        }

        public void paint(IFigure figure, Graphics graphics, Insets insets) {
            // no-op
        }
    }

    private Label makeNameLabel() {
        Label l = new Label();
        l.setFont(nameFont);
        return l;
    }

    public void setName(String str) {
        if (str == null)
            str = "";
        nameLabel.setText(str);
        nameLabel.revalidate();
    }

    public void setText(String str) {
        if (str == null)
            str = "";
        textFigure.setText(str);
        textFigure.revalidate();
    }

    public void setError(String str) {
        if (str != null && str.length() > 0) {
            addErrorFigure();
            errorFigure.setText(str);
            errorFigure.revalidate();
        } else {
            removeErrorFigure();
        }
    }

    public void setExdata(String str) {
        if (str != null && str.length() > 0) {
            addExdataFigure();
            exdataFigure.setText(str);
            exdataFigure.revalidate();
        } else {
            removeExdataFigure();
        }
    }

    public void setAlias(String str) {
        if (str == null)
            str = "";
        aliasFigure.setText(str);
        aliasFigure.revalidate();
    }

    public void setTextVisible(boolean choice) {
        textFigure.setVisible(choice);
        textFigure.revalidate();
    }

    public Label getTextLabel() {
        return textFigure.getLabel();
    }

    public List<Label> getTextLabels() {
        List<Label> textLabels = new LinkedList<Label>();
        for (CompartmentFigure c : textFigures) {
            textLabels.add(c.getLabel());
        }
        return textLabels;
    }
}
